package com.arpa.oms.controller;

import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.common.util.ArpaCloudUtil;
import com.arpa.wms.domain.consts.NumConst;
import com.arpa.wms.domain.dto.CarrierDTO;
import com.arpa.wms.domain.entity.Carrier;
import com.arpa.wms.domain.vo.CarrierVO;
import com.arpa.wms.service.ICarrierService;
import com.arpa.wms.service.TmsWaybillUpdateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 * Oms承运商 Controller
 * </p>
 *
 * @author wangdong
 * @since 2021-07-21
 */
@Api(tags = "承运商管理")
@RestController
@RequestMapping(value = "/omsCarrier", produces = "application/json;charset=UTF-8")
@Log4j2
public class OmsCarrierController {

    private final ICarrierService iCarrierService;
    private final TmsWaybillUpdateService tmsWaybillUpdateService;

    private final ArpaCloudUtil arpaCloudUtil;

    public OmsCarrierController(ICarrierService iCarrierService, TmsWaybillUpdateService tmsWaybillUpdateService, ArpaCloudUtil arpaCloudUtil) {
        this.iCarrierService = iCarrierService;
        this.tmsWaybillUpdateService = tmsWaybillUpdateService;
        this.arpaCloudUtil = arpaCloudUtil;
    }

    /**
     * 获取 承运商 列表
     *
     * @param CarrierDTO
     * @param
     * @return
     */
    @ApiOperation(value = "获取 承运商 列表", notes = "获取 承运商 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = CarrierVO.class))
    @NeedLogin
    @PostMapping("/list")
    public Result list(CarrierDTO CarrierDTO) {
        try {
            CarrierDTO.setShipmentCode(UserUtil.getLoginWarehouseCodeOrShipmentCode());
            CarrierVO CarrierVOSum = iCarrierService.queryListSum(CarrierDTO);
            List<CarrierVO> CarrierVOList = iCarrierService.queryList(CarrierDTO);

            Page returnPage = new Page(CarrierDTO.getPageNum(), CarrierDTO.getPageSize());
            returnPage.setRecords(CarrierVOList);
            returnPage.setTotal(CarrierVOSum.getTotal());
            returnPage.setSumObject(CarrierVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value = "OMS销售单获取入驻商下承运商列表", notes = "OMS销售单获取入驻商下承运商列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "shipmentCode", value = "入驻商code", dataType = "String"),
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = Carrier.class))
    @NeedLogin
    @GetMapping("/chooseData")
    public Result chooseData() {
        List<Carrier> list = iCarrierService.list(new QueryWrapper<Carrier>().lambda()
                .select(Carrier::getCode,Carrier::getName)
                .eq(Carrier::getShipmentCode, UserUtil.getLoginWarehouseCodeOrShipmentCode())
                .eq(Carrier::getStatus, NumConst.NUM_ONE)
                .orderByAsc(Carrier::getName));
        return Result.ok().data(list);
    }

}
